﻿#ifndef _MAGICSINGLETON_H_
#define _MAGICSINGLETON_H_

#include <mutex>
#include <memory>


template<typename T>
class Singleton
{
public:

	template<typename ...Args>
	static std::shared_ptr<T> GetInstance(Args&&... args)
	{
		static std::once_flag s_flag;
		std::call_once(s_flag, [&]() {
			//_pSington = std::make_shared<T>(std::forward<Args>(args)...);
			m_pSington = std::shared_ptr<T>(new T(std::forward<Args>(args)...));
		});

		return m_pSington;
	}

	static void DesInstance()
	{
		if (m_pSington)
		{
			m_pSington.reset();
			m_pSington = nullptr;
		}
	}

private:
	explicit Singleton();
	Singleton(const Singleton&) = delete;
	Singleton& operator=(const Singleton&) = delete;
	~Singleton();

private:
	static std::shared_ptr<T> m_pSington;
};

template<typename T>
std::shared_ptr<T> Singleton<T>::m_pSington = nullptr;

#endif // _MAGICSINGLETON_H_